<?php

namespace app\home\service;

use app\home\model\LimitAmountConfig;
use app\home\model\TradeLog;

/**
 * 限制玩家充值额度
 * Class LimitAmount
 * @package app\home\service
 */
class LimitAmount
{
    protected $user_id = null;
    protected $limit_config = null;

    public $current_month_start = null;
    public $current_month_end = null;
    public $current_day_start = null;
    public $current_day_end = null;

    public function __construct($user_id)
    {
        $this->user_id = $user_id;
        $this->limit_config = (new LimitAmountConfig())->getAllConf();

        $this->current_month_start = date('Y-m-01 00:00:00');
        $this->current_month_end = date('Y-m-d 23:59:59');
        $this->current_day_start = date('Y-m-d 00:00:00');
        $this->current_day_end = date('Y-m-d 23:59:59');

    }

    public function checkIsCanRecharge(){

        /**
         * 达到渠道日限额：充值失败，已达到每日充值限额！每日00:00刷新额度。
            达到渠道月限额：充值失败，已达到每月充值限额！每月1日00:00刷新额度。
            达到个人日限额：充值失败,Error:001
            达到个人月限额：充值失败,Error:002
         */
        // 个人月限额->个人日限额 或者 渠道月限额->渠道日限额
        // 判断此用户是否在个人限额配置内

        $conf_person_month = isset($this->limit_config[LimitAmountConfig::ID_PERSON_MONTH])?$this->limit_config[LimitAmountConfig::ID_PERSON_MONTH]:0;
        $conf_person_day = isset($this->limit_config[LimitAmountConfig::ID_PERSON_DAY])?$this->limit_config[LimitAmountConfig::ID_PERSON_DAY]:0;
        $conf_channel_month = isset($this->limit_config[LimitAmountConfig::ID_CHANNEL_MONTH])?$this->limit_config[LimitAmountConfig::ID_CHANNEL_MONTH]:0;
        $conf_channel_day = isset($this->limit_config[LimitAmountConfig::ID_CHANNEL_DAY])?$this->limit_config[LimitAmountConfig::ID_CHANNEL_DAY]:0;

        $flag = 1; //声明一个标志符号 来 表示是否可以购买 1表示可以购买
        $person_flag = 0;// 表示是否已经出现在个人配置里

        $trade_log = new TradeLog();
        if(!empty($conf_person_month) || !empty($conf_person_day)){
            // 个人月限额
            if(!empty($conf_person_month)){
                $person_month_users = json_decode($conf_person_month,true);
                if(array_key_exists($this->user_id,$person_month_users)){
                    $person_flag = true;
                    $amount = $person_month_users[$this->user_id];
                    $month_total = $trade_log->getAmountByTime($this->user_id,$this->current_month_start,$this->current_month_end);
                    if($amount <= $month_total){
                        mylog('LimitAmount:person_month-'.$this->user_id.'-'.$amount.'-'.$month_total);
                        $flag = -4;
                    }
                }
            }

            // 个人日限额
            if($flag == 1 && !empty($conf_person_day)){
                $person_day_users = json_decode($conf_person_day,true);
                if(array_key_exists($this->user_id,$person_day_users)){
                    $person_flag = true;
                    $amount = $person_day_users[$this->user_id];
                    $day_total = $trade_log->getAmountByTime($this->user_id,$this->current_day_start,$this->current_day_end);
                    if($amount <= $day_total){
                        mylog('LimitAmount:person_day-'.$this->user_id.'-'.$amount.'-'.$day_total);
                        $flag = -3;
                    }
                }
            }
        }

        if($person_flag == false) {
            // 如果没有配置个人限额 就继续考虑渠道限额
            // 渠道月限额
            if (!empty($conf_channel_month)) {
                $channel_month = json_decode($conf_channel_month,true);
                $channel_id = TradeLog::getChannelId($this->user_id);
                if (array_key_exists($channel_id, $channel_month)) {
                    $amount = $channel_month[$channel_id];
                    $month_total = $trade_log->getAmountByTime($this->user_id, $this->current_month_start, $this->current_month_end);
                    if ($amount <= $month_total) {
                        mylog('LimitAmount:channel_month-'.$this->user_id.'-'.$amount.'-'.$month_total);
                        $flag = -2;
                    }
                }
            }

            // 渠道日限额
            if ($flag == 1 && !empty($conf_channel_day)) {

                $channel_day = json_decode($conf_channel_day,true);
                $channel_id = TradeLog::getChannelId($this->user_id);
                if (array_key_exists($channel_id, $channel_day)) {
                    $amount = $channel_day[$channel_id];
                    $day_total = $trade_log->getAmountByTime($this->user_id, $this->current_day_start, $this->current_day_end);
                    if ($amount <= $day_total) {
                        mylog('LimitAmount:channel_day-'.$this->user_id.'-'.$amount.'-'.$day_total);
                        $flag = -1;
                    }
                }
            }
        }

        return $flag;
    }



}